package com.inter.insideinterservice;

import com.inter.entity.common.OrderPaymentManager;
import com.inter.entity.pojo.Constant;
import com.inter.entity.pojo.request.RequestBody.OpenMonthlyPackageRequestBody;
import com.inter.entity.pojo.request.RequestHeader;
import com.inter.entity.pojo.request.subRequest.OpenMonthlyPackageRequest;
import com.inter.entity.pojo.response.subResponse.OpenMonthlyPackageResponse;
import com.inter.service.PartnerService;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Demo 去北京开通流量的实现类
 *
 * @author lm
 * @date 2018/08/31
 */
public class TurnToOpenService {
    public OrderPaymentManager openPackage(String partnerCode, String deviceType, String orderId, String secret) {
        try {
            System.out.println("partnerCode是多少：" + partnerCode);
            //请求参数处理
            OpenMonthlyPackageRequestBody body = new OpenMonthlyPackageRequestBody();
            body.setDeviceType(deviceType);
            body.setOrderId(orderId);
            System.out.println("secret是多少：" + secret);
            // 请求参数拼接
            OpenMonthlyPackageRequest request = new OpenMonthlyPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_openPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OpenMonthlyPackageRequest.class).toXml(request, "utf-8");
            System.out.println("请求报文：" + sRequest);

            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            System.out.println("响应报文：" + result);
            OpenMonthlyPackageResponse responseObj = new JaxbUtil(OpenMonthlyPackageResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                System.out.println("返回结果是：" + resultInfo);
                //成功，拿到回传的结果回去处理
                OrderPaymentManager user = new OrderPaymentManager();
                user.setOrderId(responseObj.getBody().getOrderId());
                user.setOrderStartDate(responseObj.getBody().getOrderStartDate());
                user.setOrderExpireDate(responseObj.getBody().getOrderExpireDate());
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            } else {
                OrderPaymentManager user = new OrderPaymentManager();
                user.setRetCode(retCode);
                user.setResultInfo(resultInfo);
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }
}
